package cn.jbolt.ndemo.service.impl;


import cn.jbolt.ndemo.CustomerRecordLevel;
import cn.jbolt.ndemo.CustomerRecordType;
import cn.jbolt.ndemo.dao.CustomerRecordMapper;
import cn.jbolt.ndemo.entity.CustomerRecord;
import cn.jbolt.ndemo.service.CustomerRecordService;
import cn.jbolt.util.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;


@Service
public class CustomerRecordServiceImpl extends ServiceImpl<CustomerRecordMapper, CustomerRecord> implements CustomerRecordService {

    @Override
    public Result getCustomerRecordLevelList() {
        return Result.success(CustomerRecordLevel.toList());
    }

    @Override
    public Result getCustomerRecordTypeList() {
        return Result.success(CustomerRecordType.toList());
    }

    @Override
    public Result getList(String keywords) {
        QueryWrapper<CustomerRecord> queryWrapper = new QueryWrapper<>();
        if (keywords != null && !keywords.isEmpty()) {
            queryWrapper.like("customer_name", keywords)
                    .or()
                    .like("content", keywords);
        }
        queryWrapper.orderByDesc("id");
        return Result.success(list(queryWrapper));
    }
    @Override
    public Result add(CustomerRecord customerRecord) {
        boolean save = save(customerRecord);
        if (save){
            return Result.success(customerRecord);
        }
        return Result.error("添加失败");
    }

    @Override
    public Result delete(Long id) {
        boolean b = removeById(id);
        if (b){
            return Result.success();
        }
        return Result.error("删除失败");
    }
}
